package com.example.smallmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smallmall.entity.SpUser;
import com.example.smallmall.mapper.SpUserMapper;
import com.example.smallmall.service.ISpUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-19
 */
@Service
public class SpUserServiceImpl extends ServiceImpl<SpUserMapper, SpUser> implements ISpUserService {

    @Resource
    private SpUserMapper spUserMapper;
    @Override
    public Map<String,Object> login(String username, String password) {
        List<Map<String, Object>> stringObjectMap = spUserMapper.selectMaps(new QueryWrapper<SpUser>().eq("username",username).eq("password",password));
        // SpUser user = spUserMapper.login(spUser);
        Map<String, Object> objectMap = new HashMap<>();
        if (stringObjectMap != null) {
            objectMap.put("id", stringObjectMap.get(0).get("user_id"));
            objectMap.put("username", stringObjectMap.get(0).get("username"));
            objectMap.put("mobile", stringObjectMap.get(0).get("mobile"));
            objectMap.put("email", stringObjectMap.get(0).get("email"));
        }
        return objectMap;
    }

    @Override
    public Map<String, Object> selectPage(int pageNum, int pageSize) {
        Page<Map<String, Object>> page = new Page<>(pageNum, pageSize);
        // QueryWrapper<SpUser> spUserQueryWrapper = new QueryWrapper<SpUser>().select("user_id", "qq_open_id" ,"username", "user_email");
        IPage<Map<String, Object>> spUserPage = spUserMapper.selectMapsPage(page, null);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("total", spUserPage.getTotal());
        stringObjectHashMap.put("pagenum", spUserPage.getCurrent());
        stringObjectHashMap.put("users", spUserPage.getRecords());
        return stringObjectHashMap;
    }
}
